package org.bigjava.controller;

import org.bigjava.bean.Tzgg;
import org.bigjava.biz.TzggService;
import org.bigjava.common.Constant;
import org.bigjava.common.FenYe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class TzggController {
    @Autowired
    HttpSession session;
    @Autowired
    TzggService tzggService;

    //修改公告信息
    @RequestMapping("tzgg/xiugai")
    public String tzggXiuGai(Tzgg tzgg){
        Tzgg tzgga = (Tzgg) session.getAttribute("tzgg");
        tzgg.setId(tzgga.getId());
        tzggService.tzggXiuGai(tzgg);
        return "redirect:/tzgg/fenye";
    }

    //跳转到修改公告页面
    @RequestMapping("tzgg/xg")
    public String tzggXG(int id){
        System.out.println(id);
        Tzgg tzgg = tzggService.tzggId(id);
        session.setAttribute("tzgg",tzgg);
        return "tzggxg";
    }

    //根据标题或者公告内容查询信息
    @RequestMapping("tzgg/sousuo")
    public String tzggSousuo(String dyg,String deg){
        List<Tzgg>  lists = tzggService.tzggSouSuo(dyg,deg);
        session.setAttribute("tzggs",lists);
        return "tzgggl";
    }

    //删除公告
    @RequestMapping("tzgg/shanchu")
    public String tzggShanChu(int id){
        tzggService.tzggShanChu(id);
        Object a = session.getAttribute("yeshu");
        return "redirect:/tzgg/fenye?a="+a;
    }


    //发布公告
    @RequestMapping("tzgg/tianjia")
    public String tzggAdd(Tzgg tzgg){
        System.out.println(tzgg);
        tzggService.tzggAdd(tzgg);
        return "redirect:/tzgg/fenye";
    }

    //分页查询出公告信息
    @RequestMapping("tzgg/fenye")
    public String tzggFenYe(String a){
        //业主总人数
        int zong =  tzggService.tzggZong();
        session.setAttribute("zong",zong);
        //调用分页方法返回下一页开始id
        int ks = FenYe.fenYe(session,zong,a, Constant.fenye);
        //分页查询出的业主
        List<Tzgg> lists = tzggService.tzggFenYe(ks,Constant.fenye);
        session.setAttribute("tzggs",lists);
        return "tzgggl";
    }
}
